<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
class StudyStudentAction extends Model
{
    use SoftDelete;
     protected $deleteTime = false;
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        $add_time = input('param.add_time', '') ? input('param.add_time') : toDate(time(), 'Y-m-d');
        $student_id = input('param.student_id', '');
        if ($student_id) {
            $student_id = input('param.student_id');
            $where[] = ['student_id', 'in', [$student_id]];
        } else {
            $class_id = input('param.class_id', 0);
            if ($class_id) {
                $child = StudyStudentList::where('class_id', $class_id)->where('end_time', '>=', time())->select();
                $student = [];
                foreach ($child as $key => $value) {
                    $student[] = $value['id'];
                }
                $where[] = ['student_id', 'in', $student];
            }

        }


        $list = self::with([
            'get_teacher' => function($a) {
                $a->field('id,username');
            },
            'get_student' => function($a) {
                $a->field('id,username');
            }
        ])->order('id','desc')
            ->where($where)
            ->whereBetween('add_time', [strtotime($add_time . ' 00:00:00') , strtotime($add_time.' 23:59:59')])
            ->paginate($limit);
        $action = $list->items();
        foreach ($action as $key => $value) {
            $action[$key]['add_time'] = toDate($value['add_time'], 'Y-m-d H:i');
            switch ($value['action_type']) {
                case 1:
                    $data = StudentActionSituation::where('id', $value['action_id'])->find();
                    $data['situation_img'] = json_decode($data['situation_img'], true);
                    $action[$key]['action'] = $data;
                    break;
                case 2:
                    $action[$key]['action'] = StudentReplaceDiaper::where('id', $value['action_id'])->find();
                    break;
                case 3:
                    $action[$key]['action'] = StudentActionNurse::where('id', $value['action_id'])->find();
                    break;
                case 4:
                    $action[$key]['action'] = StudentActionEat::where('id', $value['action_id'])->find();
                    break;
                case 5:
                    $action[$key]['action'] = StudentActionMedication::where('id', $value['action_id'])->find();
                    break;
                case 6:
                    $action[$key]['action'] = StudentActionSleep::where('id', $value['action_id'])->find();
                    break;
                case 7:
                    $data = StudentActionClean::where('id', $value['action_id'])->find();
                    $data['clean_info'] = implode(json_decode($data['clean_info'], true));
                    $action[$key]['action'] = $data;
                    break;
                case 8:
                    $action[$key]['action'] = StudentActionDrink::where('id', $value['action_id'])->find();
                    break;
                case 9:
                    $data = StudentActionPoop::where('id', $value['action_id'])->find();
                    $data['poop_detail'] = json_decode($data['poop_detail'], true);
                    $action[$key]['action'] = $data;
                    break;
                case 10:
                    $action[$key]['action'] = StudentActionEmotion::where('id', $value['action_id'])->find();
                    break;
                case 11:
                case 12:
                    $action[$key]['action'] = StudentActionAttendance::where('id', $value['action_id'])->find();
                    break;
                case 13:
                    $action[$key]['action'] = StudentActionTemperature::where('id', $value['action_id'])->find();
                    break;
                case 14:
                    $data = StudentActionBurst::where('id', $value['action_id'])->find();
                    $data['burst_img'] = json_decode($data['burst_img'], true);
                    $action[$key]['action'] = $data;
                    break;
                case 15:
                    $data = StudentActionBehavior::where('id', $value['action_id'])->find();
                    $data['behavior_img'] = json_decode($data['behavior_img'], true);
                    $action[$key]['action'] = $data;
                    break;
                case 16:
                    $action[$key]['action'] = StudentActionAcquire::where('id', $value['action_id'])->find();
                    break;
                case 17:
                    $action[$key]['action'] = StudentActionRemarks::where('id', $value['action_id'])->find();
                    break;
                case 18:
                    $action[$key]['action'] = StudentActionAbsence::where('id', $value['action_id'])->find();
                    break;
                case 19:
                    $data = StudentActionShot::where('id', $value['action_id'])->find();
                    if ($data) {
                        $data['shot_info'] = json_decode($data['shot_info'], true);
                        $action[$key]['action'] = $data;
                    } else {
                        unset( $action[$key]);
                    }
                    break;
                case 20:
                    $data = StudentActionHealthy::where('id', $value['action_id'])->find();
                    $data['healthy_info'] = json_decode($data['healthy_info'], true);
                    $action[$key]['action'] = $data;
                    break;
                case 21:
                    $data = StudentActionStudy::where('id', $value['action_id'])->find();
                    $data['study_img'] = json_decode($data['study_img'], true);
                    $action[$key]['action'] = $data;
                    break;
                case 22:
                    $action[$key]['action'] = StudentActionClothes::where('id', $value['action_id'])->find();
                    break;
                case 23:
                    $action[$key]['action'] = StudentActionToilet::where('id', $value['action_id'])->find();
                    break;
                case 24:
                    $data = StudentActionDairy::where('id', $value['action_id'])->find();
                    $data['dairy_img'] = json_decode($data['dairy_img'], true);
                    $action[$key]['action'] = $data;
                    break;
                default:
                    $action[$key]['action'] = [];
                    break;
            }
        }
        return ['code'=>0,'data'=>$action,'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }




    public function getTeacher()
    {
        return $this->hasOne(AdminAdmin::class, 'id', 'admin_id');
    }

    public function getStudent()
    {
        return $this->hasOne(StudyStudentList::class, 'id', 'student_id');
    }
}
